;
var request = require("request");
var hepler = require("../../javascript/helper");
var format = require("string-format");
var db = require("../../javascript/mysql");
var mysql = new db.mysqlClient("accident");
var spider = function () {
    var that = this
    this.name = "出行保"
    // 生成产品数据
    this.genProductInfo = function () {
        let projects = [];
        plan = {
            planA: {
                "year": ["0-80"],
                "days": ["1d", "2d", "3d", "4d", "5d", "6d", "7d", "10d", "15d", "20d", "30d"],
                "people_count": ["1"],
                "airplane_death": ["500000"],
                "passenger_death": ["300000"],
                "ship_death": ["300000"],
                "train_death": ["300000"],
                "accident_death": ["200000"],
                "accident_medical": ["20000"],
                "accident_hospital": ["9000"]

            },
            planB: {
                "year": ["0-80"],
                "days": ["1d", "2d", "3d", "4d", "5d", "6d", "7d", "10d", "15d", "20d", "30d"],
                "people_count": ["1"],
                "airplane_death": ["800000"],
                "passenger_death": ["400000"],
                "ship_death": ["400000"],
                "train_death": ["400000"],
                "accident_death": ["300000"],
                "accident_medical": ["50000"],
                "accident_hospital": ["14400"]

            },
            planC: {
                "year": ["0-80"],
                "days": ["1d", "2d", "3d", "4d", "5d", "6d", "7d", "10d", "15d", "20d", "30d"],
                "people_count": ["1"],
                "airplane_death": ["1000000"],
                "passenger_death": ["500000"],
                "ship_death": ["500000"],
                "train_death": ["500000"],
                "accident_death": ["500000"],
                "accident_medical": ["100000"],
                "accident_hospital": ["18000"]

            }
        }
        for (k in plan) {
            data = hepler.product([plan[k]["year"], plan[k]["days"], plan[k]["people_count"], plan[k]["airplane_death"], plan[k]["passenger_death"], plan[k]["ship_death"], plan[k]["train_death"], plan[k]["accident_death"], plan[k]["accident_medical"], plan[k]["accident_hospital"]])
            for (i = 0; i < data.length; i++) {
                dt = hepler.formatProductInfo(data[i], plan[k]);
                projects.push(dt);

            }
        };
        return projects;
    }
    //爬虫
    this.httpRequest = function (dt) {
        if (dt.airplane_death == "500000") {
            params = {
                "pID": "YWQGWX10005",
                "accProdCode": "CP060010",
                "startTime": "2020-05-28",
                "endTime": "",
                "insProLimitTime": format("{0}@日", dt.days.replace("d", "")),
                "age": "0-80",
                "insurants": "1",
                "proCgyCde": "",
                "travelDestination": "云南",
                "cvrgs": '[{"cvrgNo":"YW001001","cvrgName":"交通工具意外伤害保险","liabNo":"ZYW0009","liabName":"交通意外身故/残疾（乘坐客运民航班机）","liabAmt":500000},{"cvrgNo":"YW001001","cvrgName":"交通工具意外伤害保险","liabNo":"ZYW0006","liabName":"交通意外身故/残疾（乘坐客运机动车）","liabAmt":300000},{"cvrgNo":"YW001001","cvrgName":"交通工具意外伤害保险","liabNo":"ZYW0007","liabName":"交通意外身故/残疾（乘坐客运轨道交通车辆）","liabAmt":300000},{"cvrgNo":"YW001001","cvrgName":"交通工具意外伤害保险","liabNo":"ZYW0008","liabName":"交通意外身故/残疾（乘坐客运轮船）","liabAmt":300000},{"cvrgNo":"YW007001","cvrgName":"境内旅游意外伤害保险","liabNo":"ZYW0001","liabName":"意外身故/残疾","liabAmt":200000},{"cvrgNo":"JK007001","cvrgName":"附加意外伤害医疗保险B款","liabNo":"ZJK0001","liabName":"意外医疗","liabAmt":20000},{"cvrgNo":"JK016001","cvrgName":"附加住院津贴保险","liabNo":"ZJK0002","liabName":"意外住院津贴","liabAmt":9000}]'
            }
        } else if (dt.airplane_death == "800000") {
            params = {
                "pID": "YWQGWX10006",
                "accProdCode": "CP060010",
                "startTime": "2020-05-28",
                "endTime": "",
                "insProLimitTime": format("{0}@日", dt.days.replace("d", "")),
                "age": "0-80",
                "insurants": "1",
                "proCgyCde": "",
                "travelDestination": "云南",
                "cvrgs": '[{"cvrgNo":"YW001001","cvrgName":"交通工具意外伤害保险","liabNo":"ZYW0009","liabName":"交通意外身故/残疾（乘坐客运民航班机）","liabAmt":800000},{"cvrgNo":"YW001001","cvrgName":"交通工具意外伤害保险","liabNo":"ZYW0007","liabName":"交通意外身故/残疾（乘坐客运轨道交通车辆）","liabAmt":400000},{"cvrgNo":"YW001001","cvrgName":"交通工具意外伤害保险","liabNo":"ZYW0006","liabName":"交通意外身故/残疾（乘坐客运机动车）","liabAmt":400000},{"cvrgNo":"YW001001","cvrgName":"交通工具意外伤害保险","liabNo":"ZYW0008","liabName":"交通意外身故/残疾（乘坐客运轮船）","liabAmt":400000},{"cvrgNo":"YW007001","cvrgName":"境内旅游意外伤害保险","liabNo":"ZYW0001","liabName":"意外身故/残疾","liabAmt":300000},{"cvrgNo":"JK007001","cvrgName":"附加意外伤害医疗保险B款","liabNo":"ZJK0001","liabName":"意外医疗","liabAmt":50000},{"cvrgNo":"JK016001","cvrgName":"附加住院津贴保险","liabNo":"ZJK0002","liabName":"意外住院津贴","liabAmt":14400}]'
            }

        } else {
            params = {
                "pID": "YWQGWX10007",
                "accProdCode": "CP060010",
                "startTime": "2020-05-28",
                "endTime": "",
                "insProLimitTime": format("{0}@日", dt.days.replace("d", "")),
                "age": "0-80",
                "insurants": "1",
                "proCgyCde": "",
                "travelDestination": "云南",
                "cvrgs": '[{"cvrgNo":"YW001001","cvrgName":"交通工具意外伤害保险","liabNo":"ZYW0009","liabName":"交通意外身故/残疾（乘坐客运民航班机）","liabAmt":1000000},{"cvrgNo":"YW001001","cvrgName":"交通工具意外伤害保险","liabNo":"ZYW0008","liabName":"交通意外身故/残疾（乘坐客运轮船）","liabAmt":500000},{"cvrgNo":"YW001001","cvrgName":"交通工具意外伤害保险","liabNo":"ZYW0006","liabName":"交通意外身故/残疾（乘坐客运机动车）","liabAmt":500000},{"cvrgNo":"YW001001","cvrgName":"交通工具意外伤害保险","liabNo":"ZYW0007","liabName":"交通意外身故/残疾（乘坐客运轨道交通车辆）","liabAmt":500000},{"cvrgNo":"YW007001","cvrgName":"境内旅游意外伤害保险","liabNo":"ZYW0001","liabName":"意外身故/残疾","liabAmt":500000},{"cvrgNo":"JK007001","cvrgName":"附加意外伤害医疗保险B款","liabNo":"ZJK0001","liabName":"意外医疗","liabAmt":100000},{"cvrgNo":"JK016001","cvrgName":"附加住院津贴保险","liabNo":"ZJK0002","liabName":"意外住院津贴","liabAmt":18000}]'
            }

        }
        return new Promise(function (resolve, reject) {
            request({
                url: "http://shop.ejintai.com/ecs_shop/acc/getPremium.do",
                method: "POST",
                json: false,
                headers: {
                    "Accept-Language": "zh-CN,zh;q=0.9",
                    "Connection": "keep-alive",
                    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
                    "Cookie": "route=2fee4343d7bbb9e3b1ac8fef8e5de288; JSESSIONID=2E6AF9078F58F455AF2332434CD30AA5; Hm_lvt_0b8f42721a29d8a9447a9265e5a508a7=1590565602,1590566106,1590566168,1590566188; Hm_lvt_f76c97c42cb89e5355cb571c5d9f3ba7=1590565606,1590566109,1590566170,1590566189; Hm_lpvt_f76c97c42cb89e5355cb571c5d9f3ba7=1590566189; Hm_lpvt_0b8f42721a29d8a9447a9265e5a508a7=1590566189",
                    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36",
                    "X-Requested-With": "XMLHttpRequest",
                },
                rejectUnauthorized: false,
                body: hepler.urlencode(params)
            }, function (err, res, body) {
                if (!err && res.statusCode == 200) {
                    if (body.indexOf("disPrm" != -1)) {
                        price = JSON.parse(body).disPrm.toFixed(2).toString();
                        rs = {
                            name: that.name,
                            company: "锦泰保险",
                            price: price
                        }
                        resolve(rs);
                    }
                }
            })
        })
    }
    // 执行爬虫
    this.main = async function () {
        data = that.genProductInfo();
        for (i = 0; i < data.length; i++) {
            dt = data[i]
            let rs = await that.httpRequest(dt);
            result = Object.assign(dt, rs);
            mysql.insertData( result );
        }
        mysql.close();

    }


}
